package com.springboot.controller;

import com.springboot.annotation.SimplePrint;
import com.springboot.domain.Test;
import com.springboot.domain.User;
import com.springboot.service.TestServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tc")
public class TestController extends BaseController<Test>{

    @Resource(name = "testServiceI")
    private TestServiceI tsi;

    @Override
    public List<Test> find(Test test){
        return tsi.find(test);
    }

    @RequestMapping("/findByName")
    @ResponseBody
    public List<Test> findByName(Test test){
        return tsi.findByName(test.getName());
    }

    @RequestMapping("/queryByName")
    @ResponseBody
    public List<Test> queryByName(Test test){
        return tsi.queryByNameHQL(test.getName(),test.getSex());
    }


    @Override
    public Map<String, Object> add(Test test) {
        return tsi.add(test,"hqj123");
    }

    @RequestMapping("/addTest")
    @ResponseBody
    public Map<String, Object> addTest(Test test,HttpSession session) {
        session.setAttribute("userId","hqj147");
        return tsi.add(test,"hqj123");
    }

    @RequestMapping("/editNameById")
    @ResponseBody
    @SimplePrint("123")
    public  Map<String, Object> editNameById(Test test){
        return tsi.updateNameByIdHQL(test.getName(),test.getId());
    }

    @RequestMapping("/t1")
    public String test1(ModelMap mmap){
        mmap.addAttribute("a","http://www.baidu.com");
        mmap.addAttribute("sex","1");
        List<User> list = new ArrayList<>();
        list.add(new User("hqj123","1",28,1.75));
        list.add(new User("hqjabc","2",38,1.55));
        list.add(new User("abc123","2",23,1.52));
        list.add(new User("123abc","1",28,1.78));
        mmap.addAttribute("users",list);
        return "/test1.html";
    }

}
